<!DOCTYPE html>





<html class="theme-next muse use-motion" lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="generator" content="Hexo 3.9.0">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.3.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.3.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.3.0">
  <link rel="mask-icon" href="/images/logo.svg?v=7.3.0" color="#222">

<link rel="stylesheet" href="/css/main.css?v=7.3.0">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '7.3.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    save_scroll: false,
    copycode: {"enable":false,"show_result":false,"style":null},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: '',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    }
  };
</script>

  <meta name="description" content="运行效果：">
<meta name="keywords" content="树莓派">
<meta property="og:type" content="article">
<meta property="og:title" content="树莓派从DHT11温度湿度传感器读取数据">
<meta property="og:url" content="http://yoursite.com/2018/02/01/树莓派从DHT11温度湿度传感器读取数据/index.html">
<meta property="og:site_name" content="XuQi&#39;s Blog">
<meta property="og:description" content="运行效果：">
<meta property="og:locale" content="zh-CN">
<meta property="og:image" content="http://cl.ly/400Z250L2H1y/Image%202016-05-07%20at%205.32.43%20%E4%B8%8B%E5%8D%88.png">
<meta property="og:image" content="http://cl.ly/0O1R2m101A3o/Image%202016-05-07%20at%206.46.57%20%E4%B8%8B%E5%8D%88.png">
<meta property="og:updated_time" content="2019-10-20T13:23:23.049Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="树莓派从DHT11温度湿度传感器读取数据">
<meta name="twitter:description" content="运行效果：">
<meta name="twitter:image" content="http://cl.ly/400Z250L2H1y/Image%202016-05-07%20at%205.32.43%20%E4%B8%8B%E5%8D%88.png">
  <link rel="canonical" href="http://yoursite.com/2018/02/01/树莓派从DHT11温度湿度传感器读取数据/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>树莓派从DHT11温度湿度传感器读取数据 | XuQi's Blog</title>
  








  <noscript>
  <style>
  .use-motion .motion-element,
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-title { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">

  <div class="container sidebar-position-left">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta">

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">XuQi's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/" rel="section"><i class="menu-item-icon fa fa-fw fa-home"></i> <br>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/archives/" rel="section"><i class="menu-item-icon fa fa-fw fa-archive"></i> <br>归档</a>

  </li>
  </ul>

    

</nav>
</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content page-post-detail">
            

  <div id="posts" class="posts-expand">
    

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/02/01/树莓派从DHT11温度湿度传感器读取数据/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="XuQi">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="XuQi's Blog">
    </span>
      <header class="post-header">

        
          <h1 class="post-title" itemprop="name headline">树莓派从DHT11温度湿度传感器读取数据

            
          </h1>
        

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2018-02-01 20:41:47" itemprop="dateCreated datePublished" datetime="2018-02-01T20:41:47+00:00">2018-02-01</time>
            </span>
          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2019-10-20 13:23:23" itemprop="dateModified" datetime="2019-10-20T13:23:23+00:00">2019-10-20</time>
              </span>
            
          

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>运行效果：</p>
<p><img src="http://cl.ly/400Z250L2H1y/Image%202016-05-07%20at%205.32.43%20%E4%B8%8B%E5%8D%88.png" alt></p>
<a id="more"></a>

<p> 参照：<br> <a href>http://shumeipai.nxez.com/2014/10/10/raspberry-dht11-get-temperature-data.html</a></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br></pre></td><td class="code"><pre><span class="line"> # -*- coding:utf8 -*-</span><br><span class="line">import RPi.GPIO as GPIO</span><br><span class="line">import time</span><br><span class="line"></span><br><span class="line">class Sense_DHT11():</span><br><span class="line">    def __init__(self,out_pin):</span><br><span class="line"></span><br><span class="line">        self.pin = out_pin</span><br><span class="line">        GPIO.setwarnings(False)</span><br><span class="line">        GPIO.setmode(GPIO.BCM)</span><br><span class="line">        GPIO.setup(out_pin,GPIO.OUT)</span><br><span class="line">        GPIO.output(out_pin,GPIO.HIGH)</span><br><span class="line">        self.temperature = None</span><br><span class="line">        self.humidity = None</span><br><span class="line"></span><br><span class="line">    def start(self):</span><br><span class="line">        while True:</span><br><span class="line">            self.temperature = None</span><br><span class="line">            self.humidity = None</span><br><span class="line">            # 主机复位信号</span><br><span class="line">            #发送开始信号</span><br><span class="line">            GPIO.setup(self.pin,GPIO.OUT)</span><br><span class="line">            GPIO.output(self.pin,GPIO.LOW)</span><br><span class="line">            #至少等待18us</span><br><span class="line">            time.sleep(0.01)</span><br><span class="line"></span><br><span class="line">            #拉高20-40 us</span><br><span class="line">            GPIO.output(self.pin,GPIO.HIGH)</span><br><span class="line"></span><br><span class="line">            GPIO.setup(self.pin,GPIO.IN)</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">            # 等待低电平</span><br><span class="line">            while GPIO.input(self.pin)==1:</span><br><span class="line">                continue</span><br><span class="line">            # 等待高电平</span><br><span class="line">            while GPIO.input(self.pin)==0:</span><br><span class="line">                continue</span><br><span class="line"></span><br><span class="line">            # 等待低电平,数据开始</span><br><span class="line">            while GPIO.input(self.pin)==1:</span><br><span class="line">                continue</span><br><span class="line">            # 数据位</span><br><span class="line">            j = 0</span><br><span class="line">            data = []</span><br><span class="line">            # 前40位是数据位</span><br><span class="line">            while j &lt; 40:</span><br><span class="line">                k = 0</span><br><span class="line">                # 所有数据位都是以低电平开始的</span><br><span class="line">                while GPIO.input(self.pin) == 0:</span><br><span class="line">                    continue</span><br><span class="line"></span><br><span class="line">                while GPIO.input(self.pin) == 1:</span><br><span class="line">                    k+=1</span><br><span class="line">                    # 这个20 也要根据实际情况修正</span><br><span class="line">                    if k &gt; 20:break;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">                # 可能时间会不一样,所以可以改一下3这个值</span><br><span class="line">                if k &lt; 10:</span><br><span class="line">                    # 数字1表示的时间为26-28us</span><br><span class="line">                    data.append(0)</span><br><span class="line">                else:</span><br><span class="line">                    # 数字0表示的时间为116-118us</span><br><span class="line">                    data.append(1)</span><br><span class="line"></span><br><span class="line">                j += 1</span><br><span class="line">            # 湿度整数位</span><br><span class="line">            humidity_bit=data[0:8]</span><br><span class="line">            # 湿度小数位</span><br><span class="line">            humidity_point_bit=data[8:16]</span><br><span class="line">            # 温度整数位</span><br><span class="line">            temperature_bit=data[16:24]</span><br><span class="line">            # 温度小数位</span><br><span class="line">            temperature_point_bit=data[24:32]</span><br><span class="line">            # 效验位</span><br><span class="line">            check_bit=data[32:40]</span><br><span class="line"></span><br><span class="line">            humidity=0</span><br><span class="line">            humidity_point=0</span><br><span class="line">            temperature=0</span><br><span class="line">            temperature_point=0</span><br><span class="line">            check=0</span><br><span class="line">            # 转成10进制</span><br><span class="line">            for i in range(8):</span><br><span class="line">                humidity+=humidity_bit[i]*2**(7-i)</span><br><span class="line">                humidity_point+=humidity_point_bit[i]*2**(7-i)</span><br><span class="line">                temperature+=temperature_bit[i]*2**(7-i)</span><br><span class="line">                temperature_point+=temperature_point_bit[i]*2**(7-i)</span><br><span class="line">                check+=check_bit[i]*2**(7-i)</span><br><span class="line"></span><br><span class="line">            #进行效验</span><br><span class="line">            tmp=humidity+humidity_point+temperature+temperature_point</span><br><span class="line">            if check==tmp:</span><br><span class="line">                self.temperature = &quot;%s.%s&quot; % (temperature,temperature_point)</span><br><span class="line">                self.humidity = &quot;%s.%s&quot; % (humidity,humidity_point)</span><br><span class="line">                print &quot;温度:&quot; + self.temperature + &quot;湿度:&quot; + self.humidity</span><br><span class="line">                return (self.temperature,self.humidity)</span><br><span class="line">            else:</span><br><span class="line">                time.sleep(0.5)</span><br><span class="line">                print &quot;please wait&quot;</span><br><span class="line"></span><br><span class="line">    def __del__(self):</span><br><span class="line">        pass</span><br></pre></td></tr></table></figure>

<p><strong>注意点：</strong></p>
<ol>
<li>连续采样不能间隔太短，我是500ms</li>
<li>判断采样是0，还是1是通过循环次数，所以这个需要自己调整</li>
<li>判断1的最大循环次数不能太多，太多会导致在跳不出循环。</li>
</ol>
<p>树莓派连接方法：</p>
<p><img src="http://cl.ly/0O1R2m101A3o/Image%202016-05-07%20at%206.46.57%20%E4%B8%8B%E5%8D%88.png" alt></p>

    </div>

    
    
    
        
      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/tags/树莓派/" rel="tag"># 树莓派</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/2018/01/31/Python计算机视觉第一章/" rel="next" title="Python计算机视觉编程第一章">
                  <i class="fa fa-chevron-left"></i> Python计算机视觉编程第一章
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/2018/02/01/网络Tcp-reset消息/" rel="prev" title="TCP reset及原因">
                  TCP reset及原因 <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      <div class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">

          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">XuQi</p>
  <div class="site-description motion-element" itemprop="description"></div>
</div>
  <nav class="site-state motion-element">
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">44</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
        
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
          
        
        <span class="site-state-item-count">30</span>
        <span class="site-state-item-name">标签</span>
        
      </div>
    
  </nav>



        </div>
      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">XuQi</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.9.0</div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> v7.3.0</div>

        








        
      </div>
    </footer>
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
      </div>

    

  </div>

  
  <script src="/lib/jquery/index.js?v=3.4.1"></script>
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>

<script src="/js/utils.js?v=7.3.0"></script>
  <script src="/js/motion.js?v=7.3.0"></script>


  <script src="/js/schemes/muse.js?v=7.3.0"></script>


<script src="/js/next-boot.js?v=7.3.0"></script>




  




























  

  

  


  
  <script src="/js/scrollspy.js?v=7.3.0"></script>
<script src="/js/post-details.js?v=7.3.0"></script>


</body>
</html>
